/ Assembly List / LJCDBDataAccess / DbDataAccess / Update

Namespace - LJCDBDataAccess


Returns

The result XML message.

Syntax

C#
private DbResult Update()

Updates a database record. (E)

Remarks

This method is called from the Execute() method if the DbRequest.QueryType is "Update". (E)

Example

C#
// Updates a record.
private static void Update(DbDataAccess dbDataAccess
  , DbColumns dataDefinition)
{
  // Create the list of included columns.
  // This list must not include the database assigned columns
  // or the database assigned columns must have the AutoIncrement
  // value set to "true".
  List<string> columnNames = new List<string>();
  foreach (DbColumn column in dataDefinition)
  {
    if (column.ColumnName != "Id")
    {
      columnNames.Add(column.ColumnName);
    }
  }

  Person dataRecord = new Person()
  {
    Name = "TestNameUpdated"
  };

  // Create a Data Columns object with the included data definitions
  // and values from the data record.
  var dataColumns = DbCommon.QueryDataColumns(dataRecord
    , dataDefinition, columnNames);

  // Create Key Columns.
  var keyColumns = new DbColumns()
  {
    { "Name}, (object)"TestName" }
  };
  dataDefinition.LJCSetExcludeKeyValue("PrincipleFlag");

  var queryKeyColumns = DbCommon.QueryKeys(keyColumns, dataDefinition);

  // Create query with columns containing values from the record.
  // The updated columns must not include the Db assigned columns.
  DbRequest dbRequest = new DbRequest()
  {
    Columns = dataColumns,
    KeyColumns = queryKeyColumns
    QueryType = QueryType.Update.ToString(),
    TableName = "PersonTest",
  };
  DbResult dbResult = dbDataAccess.Execute(dbRequest);

  if (dbResult != null)
  {
    string sqlStatement = dbResult.ExecutedSql;
    int affectedCount = dbResult.AffectedRecords;
  }
  return retValue;
}